<?php
/**
 * Created by PhpStorm.
 * User: UGLY
 * Date: 2017/12/5
 * Time: 14:58
 */

namespace app\blog\controller;

use think\Db;
use cmf\controller\HomeBaseController;
use think\Request;
use think\Validate;

class IndexController extends HomeBaseController
{
    private $message;
    private $datebase;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->message = Db::name('message');
        $this->datebase = Db::name('simplearticle');
    }

    public function __destruct()
    {
        unset($this->message);
        unset($this->datebase);
    }

    //前台主页
    public function index()
    {
        $title = '龙窝Luck';
        $newFiveMessage = $this->message->order('id','desc')->limit(5)->select();
        $listFiveArticle = $this->datebase->order('jw_id','desc')->limit(3)->select();
        $this->assign(['title'=>$title,'fiveMessage'=>$newFiveMessage,'data'=>$listFiveArticle]);
        return $this->fetch();
    }
    //关于我
    public function callus()
    {
        $title = '龙窝Luck - 关于我';
        $this->assign(['title'=>$title]);
        return $this->fetch();
    }
    //接受建议方法
    public function agreetext()
    {
        $request = request();
        if($request->isAjax()){
            $post = $request->post();
            //要验证的字段
            $rule = [
                    'name' => 'require|max:10',
                    'phone'=> 'require|number',
                    'content'=> 'require'
                ];
            //提交的数据
            $data = [
                'name'=>$post['username'],
                'phone'=>$post['phone'],
                'content'=>$post['content'],
                'time'=>date("Y-m-d H:i:s",time())
            ];
            //出错的信息
            $msg = [
                'name.require' => '请填写您的名称',
                'name.max'     => '名称最多不能超过10个字符',
                'phone.require'   => '请填写您的联系方式',
                'phone.number'  => '请填写正确的联系方式',
                'content'        => '请填写您要告诉我的话',
            ];
            $validate = new Validate($rule,$msg);
            if($validate->check($data)){
                if(Db::name('callus')->insert($data)){
                    echo 1;
                }else{
                    echo 2;
                }
            }else{
                echo $validate->getError();
            }
        }else{
            echo '好好的，别闹！';
        }
    }
}